package com.webmuseum.client;

//import com.gargoylesoftware.htmlunit.javascript.host.Window;
import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.NamedNodeMap;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.InlineHyperlink;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;


public class WebMuseumApp implements EntryPoint {
	
	private VerticalPanel mContentPanel;
	
	private static enum mNavigationStatusEnum { NAV_MAIN_STATUS,
																				NAV_ACCOUNT_STATUS,
																				NAV_SEARCH_STATUS, 
																				NAV_CREATE_STATUS,
																				};
																				
	
	private mNavigationStatusEnum mNavigationStatus;
	
	//Composite
	private static enum mCompositeEnum { COMPOSITE_ACCOUNT_INDEX,
																		COMPOSITE_SEARCH_INDEX,
																		COMPOSITE_CREATE_INDEX,
																	  };
	
	List<Composite> mCompositeList;
	
	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	public static final WebMuseumServiceAsync webService = GWT.create(WebMuseumService.class);
	
	
	
	
	@Override
	public void onModuleLoad() {
		
		mNavigationStatus = mNavigationStatusEnum.NAV_MAIN_STATUS;
	    RootPanel rootPanel = RootPanel.get();
	    
	    HorizontalPanel mAppPanel = new HorizontalPanel();
	    rootPanel.add(mAppPanel, 10, 10);
	    mAppPanel.setSize("837px", "431px");
	    
	    VerticalPanel verticalPanel = new VerticalPanel();
	    mAppPanel.add(verticalPanel);
	    
	    DisclosurePanel mAccountPanel = new DisclosurePanel("Account", false);
	    mAccountPanel.setAnimationEnabled(true);
	    verticalPanel.add(mAccountPanel);
	    mAccountPanel.setWidth("143px");
	    
	    FlowPanel mVerticalAccount = new FlowPanel();
	    mAccountPanel.setContent(mVerticalAccount);
	    mVerticalAccount.setSize("139px", "56px");
	    
	    PushButton lblLogin = new PushButton("Login");
	    mVerticalAccount.add(lblLogin);
	    
	    PushButton lblRegister = new PushButton("Register");
	    mVerticalAccount.add(lblRegister);
	    
	    DisclosurePanel mCardManagmentPanel = new DisclosurePanel("Card Management", false);
	    mCardManagmentPanel.setAnimationEnabled(true);
	    verticalPanel.add(mCardManagmentPanel);
	    mCardManagmentPanel.setWidth("157px");
	    
	    FlowPanel flowPanel = new FlowPanel();
	    mCardManagmentPanel.setContent(flowPanel);
	    flowPanel.setSize("134px", "4cm");
	    
	    PushButton lblSearch = new PushButton("Search");
	    flowPanel.add(lblSearch);
	    
	    PushButton lblCreate = new PushButton("Create");
	    lblCreate.addClickHandler(new ClickHandler() {
	    	public void onClick(ClickEvent event) 
	    	{
	    		((CreateComposite)mCompositeList.get(mCompositeEnum.COMPOSITE_CREATE_INDEX.ordinal() )).refresh();
	    		switchComposite(mNavigationStatusEnum.NAV_CREATE_STATUS, mCompositeEnum.COMPOSITE_CREATE_INDEX );
	    	}
	    });
	    flowPanel.add(lblCreate);
	    
	    mContentPanel = new VerticalPanel();
	    mAppPanel.add(mContentPanel);
	    mContentPanel.setSize("674px", "424px");

	    //Init Other things...
	    //Init Composite
	    mCompositeList = new ArrayList<Composite>();
	    mCompositeList.add(mCompositeEnum.COMPOSITE_ACCOUNT_INDEX.ordinal(), new LoginComposite());
	    mCompositeList.add(mCompositeEnum.COMPOSITE_SEARCH_INDEX.ordinal(), new SearchComposite());
	    mCompositeList.add(mCompositeEnum.COMPOSITE_CREATE_INDEX.ordinal(), new CreateComposite());
	    
	    //Init CardFactory
	    CardFactory.getInstance().init();
	    
	}
	
	protected void switchComposite(mNavigationStatusEnum aNavigationStatus,
			mCompositeEnum aCompositeIndex) 
	{
		if(mNavigationStatus != aNavigationStatus)
		{
			mNavigationStatus = aNavigationStatus;
			mContentPanel.clear();
			mContentPanel.add(mCompositeList.get(aCompositeIndex.ordinal()));
		}
		else
		{
			Window.alert("Already in this section!");
		}
		
	}
	
}
